package com.zhangfb95.crosschain.console.command;

import com.fasterxml.jackson.core.type.TypeReference;
import com.zhangfb95.crosschain.console.enums.CommandEnum;
import com.zhangfb95.crosschain.infra.model.Result;
import com.zhangfb95.crosschain.infra.util.json.JsonUtil;
import com.zhangfb95.crosschain.sdk.CrossChainClient;
import com.zhangfb95.crosschain.sdk.infra.model.api.request.server.rpcchainaccount.RpcChainAccountRemovePropertiesApiReq;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhangfubing
 * @since 2023/12/21
 */
@Component
public class RemoveChainAccountPropertiesCommand implements Command {

    @Autowired
    private CrossChainClient crossChainClient;
    @Autowired
    private JsonUtil jsonUtil;

    @Override
    public CommandEnum command() {
        return CommandEnum.REMOVE_CHAIN_ACCOUNT_PROPERTIES;
    }

    @Override
    public boolean handle(String[] args) {
        if (args.length < 3 || isHelp(args)) {
            printHelpInfo();
            return false;
        }

        String chainPath = args[0];
        String chainIdentity = args[1];
        String prop = args[2];
        List<String> propertyKeys = jsonUtil.readValue(prop, new TypeReference<>() {
        });
        RpcChainAccountRemovePropertiesApiReq apiReq = new RpcChainAccountRemovePropertiesApiReq();
        apiReq.setChainPath(chainPath)
                .setChainIdentity(chainIdentity)
                .setPropertyKeys(propertyKeys);

        Result<Void> apiRes = crossChainClient.removeChainAccountProperties(apiReq);
        if (apiRes.isSuccess()) {
            System.out.println("Result: success");
        } else {
            System.out.println("Result: " + apiRes.getMsg());
        }
        return false;
    }

    private void printHelpInfo() {
        System.out.println("Usage: removeChainAccountProperties [chainPath] [chainIdentity] [properties]");
        List<String> properties = new ArrayList<>();
        properties.add("key1");
        System.out.println("properties: " + jsonUtil.toJsonQuietly(properties));
    }
}
